package wangyunlong.entity;

public class PageUtils {
	public static Page getPage(int everyPage, int totalCount, int currentPage) {
		Page page = null;
		//每页显示两条



		//总页数  2                       2            4
		//总页数  6                       3            17
		int totalPage = getTotalPage(everyPage, totalCount);

		currentPage = getCurrentPage(currentPage,totalPage);
		//起始条     5                         2           3
		//起始条      11                       5           3
		//起始条      11                       3           7
		int beginIndex = getBeginIndex(everyPage, currentPage);


		boolean hasPrePage = hasPrePage(currentPage);
		boolean hasNextPage = hasNextPage(totalPage, currentPage);
		return page = new Page(everyPage, totalCount, totalPage, currentPage, beginIndex, hasPrePage, hasNextPage);
	}



	/**
	 * 设定当前页
	 *
	 * @param currentPage
	 * @return
	 */
	public static int getCurrentPage(int currentPage,int totalPage) {
		if(currentPage == 0) {
			currentPage = 1;
		}else if(currentPage > totalPage ){
			currentPage =totalPage;
		}
		return currentPage;
	}

	/**
	 * 设定分页的总页数
	 *
	 * @param everyPage
	 * @param totalCount
	 * @return
	 */
	public static int getTotalPage(int everyPage, int totalCount) {
		int num = totalCount / everyPage;
		return totalCount % everyPage == 0 ? num : num + 1;
	}

	/**
	 * 设置起始点
	 *
	 * @param everyPage
	 * @param currentPage
	 * @return
	 */
	public static int getBeginIndex(int everyPage, int currentPage) {
		return (currentPage - 1) * everyPage;
	}

	/**
	 * 设置是否有上一页
	 *
	 * @param currentPage
	 * @return
	 */
	public static boolean hasPrePage(int currentPage) {
		return currentPage == 1 ? false : true;
	}

	/**
	 * 设置是否有下一页
	 *
	 * @param currentPage
	 * @return
	 */
	public static boolean hasNextPage(int totalPage, int currentPage) {
		return currentPage == totalPage || totalPage == 0 ? false : true;
	}
}
